package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import dao.MovieDao;
import entity.Movie;
import utils.DBUtil;
import utils.DBUtil2;

public class MovieDaoImpl implements MovieDao {
	private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	
	public List<Movie> getAll() {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
//			ps=conn.prepareStatement("select * from movie limit 50");
			ps=conn.prepareStatement("select * from movie");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));				
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);			
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
	}

	public List<Movie> ranking_list() {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from ranking_movie");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
	}

	public List<Movie> today_recommend_list() {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from today_movie");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
	}

	public List<Movie> star_level_list() {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from star_movie");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
	}

	public List<Movie> random_list() {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
//			ps=conn.prepareStatement("select * from movie limit 50");
			ps=conn.prepareStatement("select * from movie");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}

		List<Movie> random_list= new ArrayList<Movie>();
		int size=list.size();
		int m=size/15;
		int sum=0;
//		List<Integer> int_list=null;
		for(int i=0;i<15&&sum<size;i++) {
			int num=(int)(Math.random()*m);	
			
			sum=i*m+num;
			random_list.add(list.get(sum));
		}
		return random_list;
	}
	
	public List<Movie> getByName(String m_title) {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from movie where m_title like ?");
			ps.setString(1, "%"+m_title+"%");
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
					
	}

	public Movie getByName2(String title) {
		Movie mo=new Movie();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from movie where m_title=?");
			ps.setString(1, title);
			rs=ps.executeQuery();
			while(rs.next()){
				mo.setId(rs.getInt("id"));
				mo.setM_title(rs.getString("m_title"));
				mo.setM_author(rs.getString("m_author"));
				mo.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				mo.setM_pub_time(dateStr);
				mo.setM_url(rs.getString("m_url"));
				mo.setM_grade(rs.getFloat("m_grade"));
				mo.setM_sdescribe(rs.getString("m_sdescribe"));
				mo.setM_ldescribe(rs.getString("m_ldescribe"));
				mo.setM_comment_num(rs.getInt("m_comment_num"));
				mo.setM_great_num(rs.getInt("m_great_num"));
				mo.setM_share_num(rs.getInt("m_share_num"));
				mo.setM_type(rs.getString("m_type"));
				mo.setM_channel(rs.getString("m_channel"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		System.out.print(mo.getM_author());
		return mo;	
	}

	public List<Movie> getByChannel(String Channel) {
		List<Movie> list=new ArrayList<Movie>();
		conn=DBUtil2.getConnection();
		ps=null;
		rs=null;
		try {
			ps=conn.prepareStatement("select * from movie where m_channel=?");
			ps.setString(1, Channel);
			rs=ps.executeQuery();
			while(rs.next()){
				Movie m=new Movie();
				m.setId(rs.getInt("id"));
				m.setM_title(rs.getString("m_title"));
				m.setM_author(rs.getString("m_author"));
				m.setM_pic(rs.getString("m_pic"));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String dateStr=sdf.format(rs.getTimestamp(5));				
				m.setM_pub_time(dateStr);
				m.setM_url(rs.getString("m_url"));
				m.setM_grade(rs.getFloat("m_grade"));
				m.setM_sdescribe(rs.getString("m_sdescribe"));
				m.setM_ldescribe(rs.getString("m_ldescribe"));
				m.setM_comment_num(rs.getInt("m_comment_num"));
				m.setM_great_num(rs.getInt("m_great_num"));
				m.setM_share_num(rs.getInt("m_share_num"));
				m.setM_type(rs.getString("m_type"));
				m.setM_channel(rs.getString("m_channel"));
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);
		}
		return list;
	}
	
}
